package com.mlog.njyjy.ndop.process.admin.service.impl;

import com.mlog.njyjy.ndop.common.domain.Node;
import com.mlog.njyjy.ndop.common.domain.NodeGroup;
import com.mlog.njyjy.ndop.persistence.dao.NodeGroupRepository;
import com.mlog.njyjy.ndop.process.admin.service.NodeGroupService;
import com.mlog.njyjy.ndop.process.admin.service.NodeService;
import com.mlog.njyjy.ndop.process.admin.support.enums.NodeRelationType;
import com.mlog.njyjy.ndop.share.sql.support.SqlTableUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author zyz
 * @version v1.0, 2020/8/18
 */
@Slf4j
@Service
public class NodeGroupServiceImpl implements NodeGroupService {

    @Autowired
    private NodeGroupRepository repository;

    @Autowired
    private NodeService nodeService;

    /**
     * 获取全部节点组
     *
     * @return
     */
    @Override
    public List<NodeGroup> findAll() {
        return repository.find("select * from " + SqlTableUtil.getTblName(NodeGroup.class) + " order by create_at asc");
    }

    /**
     * 返回节点组分组树结构
     *
     * @return
     */
    @Override
    public List<NodeGroup> treeData() {
        List<NodeGroup> groupList = findAll();
        //过滤出根节点
        return groupList.stream().filter(group -> 0 == group.getParentId())
                .map(group -> group.setChildren(childrenTreeData(group.getId())))
                .collect(Collectors.toList());
    }

    /**
     * 返回指定父节点的树结构
     *
     * @param parentId
     * @return
     */
    @Override
    public List<NodeGroup> childrenTreeData(Long parentId) {
        if (Objects.isNull(parentId)) {
            return null;
        }
        try {
            List<NodeGroup> groupList = repository.findByField("parent_id", parentId);
            return groupList.stream().peek(group ->
                    group.setChildren(childrenTreeData(group.getId()))
            ).collect(Collectors.toList());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 添加节点组
     *
     * @param baseNodeId
     * @param type
     * @param groupName
     * @return
     */
    @Override
    public boolean addNodeGroup(Long baseNodeId, NodeRelationType type, String groupName) {
        NodeGroup baseNodeGroup = repository.findOne(baseNodeId);
        if (baseNodeGroup == null) {
            log.error("nodeGroup of id [{}] does not exist ", baseNodeId);
            return false;
        }

        long parentId;
        if (NodeRelationType.brother.equals(type)) {
            if (0 == baseNodeGroup.getParentId()) {
                NodeGroup nodeGroup = new NodeGroup().setName(groupName);
                return repository.insert(nodeGroup);
            }
            parentId = baseNodeGroup.getParentId();
        } else {
            parentId = baseNodeId;
        }
        NodeGroup nodeGroup = new NodeGroup().setName(groupName).setParentId(parentId);
        return repository.insert(nodeGroup);
    }

    /**
     * 删除节点组
     *
     * @param nodeGroupId
     * @return
     */
    @Override
    public boolean deleteNodeGroup(Long nodeGroupId) {

        NodeGroup nodeGroup = repository.findOne(nodeGroupId);
        if (nodeGroup == null) {
            log.error("nodeGroup of id [{}] does not exist ", nodeGroupId);
            return false;
        }

        boolean succeed = repository.delete(nodeGroupId);
        if (succeed) {
            List<Node> nodeList = nodeGroup.getNodeList();

            //删除子节点组
            List<NodeGroup> childrenNode = nodeGroup.getChildren();
            if (childrenNode != null) {
                childrenNode.forEach(nodeGroup1 -> {
                    repository.delete(nodeGroup1.getId());
                    nodeList.addAll(nodeGroup1.getNodeList());
                });
            }

            //删除关联节点
            if (nodeList != null) {
                nodeList.forEach(node -> nodeService.delete(node.getId()));
            }

        }
        return succeed;
    }

    /**
     * 模糊查询节点组
     *
     * @param input
     * @return
     */
    @Override
    public List<NodeGroup> freezeQueryByName(String input) {

        String sql = "select * from " + SqlTableUtil.getTblName(NodeGroup.class) + " where name like '" + "%" + input + "%'";
        return repository.find(sql);
    }
}
